package com.zpark.servlet_session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 获取用户登录的用户名字
 *      从session域对象中获取
 */
@WebServlet("/get_name.do")
public class GetUsernameServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 解决响应乱码
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");


        // 获取session对象
        HttpSession session = req.getSession();

        // 获取绑定数据
        String name = (String) session.getAttribute("name");



        // 响应到浏览器
        resp.getWriter().println("<h1>" + name + "</h1>");
    }
}
